<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <home-weekend :list="weekendList"></home-weekend>
  </div>
</template>

<script>
  // import HomeHeader from './components/Header'
  import HomeSwiper from './components/Swiper'
  import HomeIcons from './components/Icons'
  import HomeRecommend from './components/Recommend'
  import HomeWeekend from './components/Weekend'
  import axios from 'axios'

  export default {
    name: 'Home',
    //引入局部组件
    components: {
      //异步加载形式
      HomeHeader: () => import('./components/Header'),
      //同步加载
      HomeSwiper: HomeSwiper,
      HomeIcons: HomeIcons,
      HomeRecommend: HomeRecommend,
      HomeWeekend: HomeWeekend,
    },
    data () {
      return {
        // city: '',
        swiperList: [],
        iconList: [],
        recommendList: [],
        weekendList: [],
        lastCity: ''
      }
    },
    methods: {
      getHomeInfo () {
        axios.get('/api/index.json?city=' + this.$store.state.city)
          .then(this.getHomeInfoSucc)
      },
      getHomeInfoSucc (res) {
        res = res.data
        if (res.ret && res.data) {
          const data = res.data
          // this.city = res.data.city
          this.swiperList = data.swiperList
          this.iconList = data.iconList
          this.weekendList = data.weekendList
          this.recommendList = data.recommendList
        }
        console.log(res)
      }
    },
    //生命周期方法
    mounted () {
      this.lastCity = this.$store.state.city
      this.getHomeInfo()
    },
    //页面重新显示
    activated () {
      //请求数据
      if (this.lastCity !== this.$store.state.city) {
        this.lastCity = this.$store.state.city
        this.getHomeInfo()
      }
    }

  }
</script>

<style scoped>
  /*.home {*/
  /*font-size: 50px;*/
  /*}*/
</style>
